package com.GroupSix.service;

        import com.GroupSix.dao.DictDao;
        import com.GroupSix.entity.DictEntity;
        import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
        import com.baomidou.mybatisplus.core.metadata.IPage;
        import com.baomidou.mybatisplus.core.toolkit.Wrappers;
        import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
        import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import org.springframework.stereotype.Service;

        import java.time.LocalDateTime;

@Service("dictService")
public class DictServiceImpl extends ServiceImpl<DictDao, DictEntity> implements DictService {
    // 增：添加字典
    @Override
    public Integer add(DictEntity dict){
        dict.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(dict);
    }
    // 删:删除操作
    @Override
    public Integer delete(Integer id) {
        return baseMapper.deleteById(id);
    }
    // 改：更新字典
    @Override
    public Integer update(DictEntity dict) {
        dict.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(dict);
    }
    //查:根据索引查询
    @Override
    public DictEntity search(Integer id) {
        LambdaQueryWrapper<DictEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DictEntity::getId, id);
        return baseMapper.selectOne(wrapper);
    }
    // 查：分页查询字典列表
    @Override
    public IPage<DictEntity> List(DictEntity dict, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<DictEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DictEntity::getDictName,dict.getDictName());
        int curpage = 1;
        int limit = 10;
        if(pageNo!=null&&pageNo>0){
            curpage = pageNo;
        }
        if(pageSize!=null&&pageSize>0){
            limit = pageSize;
        }
        IPage<DictEntity>  page = Page.of(curpage,limit);
        return baseMapper.selectPage(page,wrapper);
    }
}
